using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

namespace MicroSnake
{
    /// <summary>
    /// A custom control that represent the game board
    /// </summary>
    class GameBoard : Control
    {
        public GameItems[][] Items { get; set; }

        private Brush backgroundBrush;
        private Brush wallBrush;
        private Pen borderPen;
        private Bitmap tailBitmap;
        private Bitmap headUpBitmap;
        private Bitmap headDownBitmap;
        private Bitmap headLeftBitmap;
        private Bitmap headRightBitmap;

        public GameBoard()
        {
            this.Width = 200;
            this.Height = 200;

            backgroundBrush = new SolidColorBrush(MicroSnakeColors.GameBackground);
            wallBrush = new SolidColorBrush(MicroSnakeColors.GameWall);
            borderPen = new Pen(Colors.White);

            tailBitmap = Resources.GetBitmap(Resources.BitmapResources.tail);
            headUpBitmap = Resources.GetBitmap(Resources.BitmapResources.head_up);
            headDownBitmap = Resources.GetBitmap(Resources.BitmapResources.head_down);
            headLeftBitmap = Resources.GetBitmap(Resources.BitmapResources.head_left);
            headRightBitmap = Resources.GetBitmap(Resources.BitmapResources.head_right);

            Items = new GameItems[20][];
            for(int i=0; i<20; i++)
            {
                Items[i] = new GameItems[20];
            }
        }

        public override void OnRender(Microsoft.SPOT.Presentation.Media.DrawingContext dc)
        {
            base.OnRender(dc);
            

            dc.DrawRectangle(backgroundBrush,
                             borderPen,
                             0, 0, this.ActualWidth, this.ActualHeight);

            Bitmap b = tailBitmap;

            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (Items[i][j] == GameItems.Empty)
                    {
                        continue;
                    }
                    if (Items[i][j] == GameItems.Wall)
                    {
                        dc.DrawRectangle(wallBrush,
                                         null,
                                         10 * i, 10 * j, 10, 10);
                        continue;
                    }
                    if (Items[i][j] == GameItems.Tail)
                    {
                        b = tailBitmap;
                    }
                    else if (Items[i][j] == GameItems.HeadUp)
                    {
                        b = headUpBitmap;
                    }
                    else if (Items[i][j] == GameItems.HeadDown)
                    {
                        b = headDownBitmap;
                    }
                    else if (Items[i][j] == GameItems.HeadLeft)
                    {
                        b = headLeftBitmap;
                    }
                    else if (Items[i][j] == GameItems.HeadRight)
                    {
                        b = headRightBitmap;
                    }

                    dc.DrawImage(b, 10 * i, 10 * j, 0, 0, 10, 10);
                }
            }
        }
    }
}
